#include "usermanage.h"
#include "ui_usermanage.h"

UserManage::UserManage(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::UserManage)
{
    ui->setupUi(this);
    m_pLog = LogClass::getInstance();
    m_pSqliteManage = new SqliteManage;
    connect(ui->btnModiInfo,&QPushButton::clicked,this,&UserManage::onModiInfoBtnClicked);
    connect(ui->btnModiPasswd,&QPushButton::clicked,this,&UserManage::onModiPassBtnClicked);
}

UserManage::~UserManage()
{
    delete ui;
    m_pSqliteManage->closeDatabase();
}

void UserManage::setWidgetInfo(QString username)
{
    setUserInfo(username);
    ui->labelUsername->setText(UserName);
    ui->labelEmail->setText(email);
    ui->labelPhone->setText(phone);
    ui->labelNickname->setText(nickname);
    ui->labelRole->setText(user_role);
    ui->lineEditEmail->setText(email);
    ui->lineEditPhone->setText(phone);
    ui->lineEditNickname->setText(nickname);
}

void UserManage::setUserInfo(QString username)
{
    if (!m_pSqliteManage->openDatabase("C:/Users/shafl/Desktop/FrameWork/test.db")) {
        qDebug() << "Failed to open database:" << m_pSqliteManage->lastError();
        //emit sendMessage("数据库连接错误");
        return;
    }
    QList<QVariantMap> users = m_pSqliteManage->selectData("users","name='" + username + "'");
    foreach (const QVariantMap &user, users) {
        UserName = user["name"].toString();
        PassWord = user["passwd"].toString();
        user_role = user["role"].toString();
        email = user["email"].toString();
        phone = user["phone"].toString();
        nickname = user["nickname"].toString();
    }
}

void UserManage::onModiInfoBtnClicked()
{
    QVariantMap data;
    QString tempnickname = ui->lineEditNickname->text();
    QString tempphone = ui->lineEditPhone->text();
    QString tempemail = ui->lineEditEmail->text();
    if(tempemail.isEmpty()||tempphone.isEmpty()){
        ui->labelModiInfo->setText("邮箱和电话不能为空");
        return;
    }
    data["nickname"]= tempnickname;
    data["phone"]= tempphone;
    data["email"]= tempemail;
    QString name = UserName;
    m_pSqliteManage->updateData("users",data,"name='"+name+"'");
    ui->labelModiInfo->setText("修改成功");
    m_pLog->log(m_pSqliteManage,OPERATION_LOG,INFO,QString("用户：%1修改了个人信息").arg(UserName),m_pSqliteManage->getUserIdByName(UserName));
    setWidgetInfo(UserName);
}

void UserManage::onModiPassBtnClicked()
{
    if (!m_pSqliteManage->openDatabase("C:/Users/shafl/Desktop/FrameWork/test.db")) {
        qDebug() << "Failed to open database:" << m_pSqliteManage->lastError();
        return;
    }
    QString old_passwd = ui->lineEditOldPasswd->text();
    QString new_passwd = ui->lineEditNewPasswd->text();
    QString verify_new_passwd = ui->lineEditVerifyPasswd->text();
    if(old_passwd.isEmpty()||new_passwd.isEmpty()||verify_new_passwd.isEmpty()){
        ui->labelModiPasswd->setText("内容不能为空");
        return;
    }
    if(new_passwd != verify_new_passwd){
        ui->labelModiPasswd->setText("两次密码不一致");
        ui->lineEditNewPasswd->clear();
        ui->lineEditVerifyPasswd->clear();
        return;
    }
    QList<QVariantMap> users = m_pSqliteManage->selectData("users","name='"+UserName+"'");
    foreach (const QVariantMap &user, users) {
        UserName = user["name"].toString();
        PassWord = user["passwd"].toString();
        QString trans_old_passwd = QCryptographicHash::hash(old_passwd.toUtf8(), QCryptographicHash::Sha256).toHex();
        QString trans_line_passwd = QCryptographicHash::hash(new_passwd.toUtf8(), QCryptographicHash::Sha256).toHex();
        if(PassWord == trans_old_passwd){
            QVariantMap data;
            data["passwd"]= trans_line_passwd;
            m_pSqliteManage->updateData("users",data,"name='"+UserName+"'");
            ui->labelModiPasswd->setText("密码修改成功");
            m_pLog->log(m_pSqliteManage,OPERATION_LOG,WARNING,QString("用户：%1修改了个人密码").arg(UserName),m_pSqliteManage->getUserIdByName(UserName));
            break;
        }
        else{
            ui->labelModiPasswd->setText("原密码错误");
            break;
        }
    }
}

